function creatRequest(url, method, data, isLoading) {
    console.log(this)
    var header = ''
    var AMAP = 'https://lequer.odeman.com/weixin'
    if (method.toUpperCase() == 'POST') {

        header = {
            'Content-Type': 'application/x-www-form-urlencoded',
            'token': wx.getStorageSync('token')
        }
    } else {
        header = {
            'content-type': 'application/json',
            'token': wx.getStorageSync('token')
        }
    }
    return new Promise((resolve, reject) => {
        if (!isLoading) {
            wx.showLoading({ title: '加载中' })
        }
        console.log(AMAP + url)
        wx.request({
            url: AMAP + url,
            data: data || {},
            header,
            method: method,
            success: function (res) {
                wx.hideLoading()
                console.log(res.data)
                if (res.data.code == 0) {
                    resolve(res.data)
                } else if (res.data.code == 20000) {
                    wx.showModal({
                        title: '',
                        content: res.data.error_msg
                    })
                    resolve(res.data)
                } else if (res.data.code == 400) {
                    wx.showToast({
                        title: '登录失效,重新登录中',
                        icon: 'none',
                        duration: 2000
                      })
                    // 登录
                    wx.login({
                        success: res => {
                            // 发送 res.code 到后台换取 openId, sessionKey, unionId
                            console.log(res.code)
                            wx.request({
                                url: AMAP + '/token/gettoken', 
                                data: {
                                    code: res.code
                                },
                                header: {
                                  'content-type': 'application/json'
                                },
                                success(ret) {
                                    wx.setStorageSync('token', ret.data.token)
                                    wx.reLaunch({
                                        url: '/pages/index/index'
                                      })
                                }
                              })

                        }
                    })
                } else {
                    resolve(res.data)
                }
            },
            fail: function (err) {
                wx.hideLoading()
                wx.showToast({
                    title: '网络异常，请稍后再试',
                    icon: 'none'
                })
                reject(new Error(res))
            }
        })
    })
}
console.log(this)
function getToken(opt) {
    return creatRequest('/token/gettoken', 'get', opt, true)
}
function cityList(opt) {
    return creatRequest('/city/getAllCity', 'get', opt)
}
// 个人中心
function memberCenter(opt) {
    return creatRequest('/Minepage/index', 'post', opt)
}
// 首页数据
function indexData(opt) {
    return creatRequest('/index/index', 'get', opt)
}
// 首页上新分布请求
function pullRequest(opt) {
    return creatRequest('/index/newdata', 'get', opt, true)
}
// 推荐
function recommend(opt) {
    return creatRequest('/index/goodActive', 'get', opt, true)
}
// 周末活动
function activeWeekend(opt) {
    return creatRequest('/index/activeWeekend', 'post', opt, true)
}
//获取发布乐趣活动id
function addActive(opt) {
    return creatRequest('/Decaler/addActive', 'post', opt, true)
}
//  获取活动分类
function choseCategory(opt) {
    return creatRequest('/Decaler/choseCategory', 'get', opt)
}
//  保存乐趣尔主体内容 即发布活动
function saveActive(opt) {
    return creatRequest('/Decaler/saveActive', 'post', opt)
}
// 删除票据
function deltickts(opt) {
    return creatRequest('/Decaler/deltickts', 'get', opt)
}
// 编辑票据
function edittickts(opt) {
    return creatRequest('/Decaler/edittickts', 'post', opt)
}
// 发布票种
function decalertickts(opt) {
    return creatRequest('/Decaler/decalertickts', 'post', opt)
}
// 获取所有票种

function getAllTickts(opt) {
    return creatRequest('/Decaler/getAllTickts', 'get', opt)
}
// 获取夏令营分类
function choseCategory(opt) {
    return creatRequest('/Decaler/choseCategory', 'get', opt)
}
// 活动详情图片上传
function activeDetailUpPic(opt) {
    return creatRequest('/Decaler/upload_pic', 'post', opt)
}
// 获取活动详情
function getDescription(opt) {
    return creatRequest('/Decaler/getDescription', 'get', opt)
}

// 保存活动详情
function saveDescription(opt) {
    return creatRequest('/Decaler/saveDescription', 'post', opt)
}
// 去活动详情页面
function toActivityDetail(opt) {
    return creatRequest('/Active/getIndex', 'post', opt)
}
// 周末活动分类
function getStairClass(opt) {
    return creatRequest('/Category/getCategory', 'post', opt)
}
function getCatActive(opt) {
    return creatRequest('/Category/getCatActive', 'post', opt)
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>个人中心相关接口

// 保存用户头像和昵称
function saveUserInfo(opt) {
    return creatRequest('/Minepage/saveHeaderimg', 'post', opt)
}
// 活动信息
function getMyorder(opt) {
    return creatRequest('/Minepage/getMyorder', 'post', opt)
}
// 个人主页
function minePage(opt) {
    return creatRequest('/Minepage/minePage', 'post', opt)
}
// 关注
function addCancer(opt) {
    return creatRequest('/Minepage/addCancer', 'post', opt)
}
// 取消关注 
function quxiaoCancer(opt) {
    return creatRequest('/Minepage/quxiaoCancer', 'post', opt)
}
// 更新我的资料

function updateInfomation(opt) {
    return creatRequest('/Minepage/updateInfomation', 'post', opt)
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>详情页
// 提交订单
function submitOrder(opt) {
    return creatRequest('/WxPay/index', 'post', opt)
}
// 支付
function toPay(opt) {
    return creatRequest('/WxPay/payindex', 'post', opt)
}
// 报名管理
function applyManagement(opt) {
    return creatRequest('/Minepage/getSignInfo', 'post', opt)
}
// 分享功能
function share(opt) {
    return creatRequest('/Active/shareCallBack', 'post', opt)
}
// 获取电子发票数据
function eticket(opt) {
    return creatRequest('/Minepage/getOrderInfo', 'post', opt)
}
// 退款
function editMoney(opt) {
    return creatRequest('/Wxpay/refundindex', 'post', opt)
}
// 收入列表
function incomeList(opt) {
    return creatRequest('/Active/getListOrder', 'post', opt)
}
// 退款设置
function exitCostSet(opt) {
    return creatRequest('/Active/set_Refund', 'post', opt)
}
// 更新活动编辑
function updateActive(opt) {
    return creatRequest('/Decaler/updateActive', 'post', opt)
}
// 提现
function tixianConditon(opt) {
    return creatRequest('/Wxpay/tixian_Conditon', 'post', opt)
}
// 删除订单
function deleOrder(opt) {
    return creatRequest('/Order/del', 'post', opt)
}
// 完成活动
function finshedActive(opt) {
    return creatRequest('/order/upstatus', 'post', opt)
}
// 取消订单
function cancerOrder(opt) {
    return creatRequest('/order/cancer', 'post', opt)
}

module.exports = {
    getToken: getToken,
    cityList: cityList,
    memberCenter: memberCenter,
    indexData: indexData,
    pullRequest: pullRequest,
    recommend: recommend,
    activeWeekend: activeWeekend,
    addActive: addActive,
    choseCategory: choseCategory,
    saveActive: saveActive,
    deltickts: deltickts,
    edittickts: edittickts,
    decalertickts: decalertickts,
    getAllTickts: getAllTickts,
    choseCategory: choseCategory,
    activeDetailUpPic: activeDetailUpPic,
    getDescription: getDescription,
    saveDescription: saveDescription,
    toActivityDetail: toActivityDetail,
    getStairClass: getStairClass,
    getCatActive: getCatActive,
    saveUserInfo: saveUserInfo,
    getMyorder: getMyorder,
    minePage: minePage,
    addCancer: addCancer,
    quxiaoCancer: quxiaoCancer,
    updateInfomation: updateInfomation,
    submitOrder: submitOrder,
    toPay: toPay,
    applyManagement: applyManagement,
    share: share,
    eticket: eticket,
    editMoney: editMoney,
    incomeList: incomeList,
    exitCostSet: exitCostSet,
    updateActive: updateActive,
    tixianConditon: tixianConditon,
    deleOrder: deleOrder,
    finshedActive: finshedActive,
    cancerOrder: cancerOrder
}
console.log(this)